package org.zeapp.opencloud.ui.exception;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class ExceptionDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JLabel lblTitle = new JLabel();
	private JTextArea textArea = new JTextArea();

	/**
	 * Create the dialog.
	 */
	public ExceptionDialog() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle(Messages.getString("ExceptionDialog.this.title")); //$NON-NLS-1$
		setBounds(100, 100, 670, 375);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"), }));
		{
			JLabel lblNewLabel = new JLabel("");
			lblNewLabel
					.setIcon(new ImageIcon(
							ExceptionDialog.class
									.getResource("/javax/swing/plaf/metal/icons/ocean/error.png")));
			contentPanel.add(lblNewLabel, "2, 2, center, center");
		}
		{
			contentPanel.add(lblTitle, "4, 2, fill, default");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, "2, 4, 3, 1, fill, fill");
			{

				scrollPane.setViewportView(textArea);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						ExceptionDialog.this.setVisible(false);
						ExceptionDialog.this.dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

	public static void show(Throwable exception) {
		ExceptionDialog.show(null, exception);
	}

	public static void show(Component relativeTo, Throwable exception) {

		ExceptionDialog dlg = new ExceptionDialog();

		dlg.setLocationRelativeTo(relativeTo);
		dlg.lblTitle.setText(exception.getMessage());
		dlg.setModal(true);

		StringWriter errors = new StringWriter();
		exception.printStackTrace(new PrintWriter(errors));
		dlg.textArea.setText(errors.toString());
		dlg.setVisible(true);
	}
}
